﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

    public class DONHANG_DATAACCESS
    {
        private static string sp_Name = "SP_DONHANGTOTAL";
        public static void ExcuteQueryString(string ACTION, DONHANG_ENTITY dh)
        {
            SqlCommand com = new SqlCommand();
            DATABASE_CONTROL csdl = new DATABASE_CONTROL();
            try
            {

                com.Connection = csdl.CheckConnect();
                com.Parameters.AddWithValue("@ACTION", ACTION);
                if (dh != null)
                {
                    com.Parameters.AddWithValue("@MAHD", dh.MAHD);
                    com.Parameters.AddWithValue("@MAKH", dh.MAKH);
                    com.Parameters.AddWithValue("@NGAYHD", dh.NGAYHD);                  
                    com.Parameters.AddWithValue("@VAT",dh.VAT);
                    com.Parameters.AddWithValue("@THANHTIEN",dh.THANHTIEN);

                }
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = sp_Name;
                if (com.Connection.State == ConnectionState.Closed)
                {
                    com.Connection.Open();
                }
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                com.Connection.Close();
            }
        }
        public static DataSet GETDATATABLE(string ACTION, DONHANG_ENTITY dh)
        {
            SqlCommand com = new SqlCommand();
            DATABASE_CONTROL csdl = new DATABASE_CONTROL();
            DataSet ds;
            try
            {

                com.Connection = csdl.CheckConnect();
                com.Parameters.AddWithValue("@ACTION", ACTION);
                if (dh != null)
                {
                    com.Parameters.AddWithValue("@MAHD", dh.MAHD);
                    com.Parameters.AddWithValue("@MAKH", dh.MAKH);
                    com.Parameters.AddWithValue("@NGAYHD", dh.NGAYHD);
                    com.Parameters.AddWithValue("@VAT", dh.VAT);
                    com.Parameters.AddWithValue("@THANHTIEN", dh.THANHTIEN);

                }
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = sp_Name;
                if (com.Connection.State == ConnectionState.Closed)
                {
                    com.Connection.Open();
                }
                ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                return new DataSet();
                throw ex;
            }
            finally
            {
                com.Connection.Close();
            }
        }


        private static string sp_Name1 = "DH_PHIEUXUAT";
        public static DataSet GETDATATABLENONE(string ACTION)
        {
            SqlCommand com = new SqlCommand();
            DATABASE_CONTROL csdl = new DATABASE_CONTROL();
            DataSet ds;
            try
            {

                com.Connection = csdl.CheckConnect();
                com.Parameters.AddWithValue("@ACTION", ACTION);
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = sp_Name1;
                if (com.Connection.State == ConnectionState.Closed)
                {
                    com.Connection.Open();
                }
                ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                return new DataSet();
                throw ex;
            }
            finally
            {
                com.Connection.Close();
            }
        }
        //public static DataSet GETDATATABLE(string ACTION, DONHANG_ENTITY dh)
        //{
        //    SqlCommand com = new SqlCommand();
        //    DATABASE_CONTROL csdl = new DATABASE_CONTROL();
        //    DataSet ds;
        //    try
        //    {

        //        com.Connection = csdl.CheckConnect();
        //        com.Parameters.AddWithValue("@ACTION", ACTION);
        //        if (dh != null)
        //        {
        //            com.Parameters.AddWithValue("@MAXE", dh.MAHD);

        //        }
        //        com.CommandType = CommandType.StoredProcedure;
        //        com.CommandText = sp_Name1;
        //        if (com.Connection.State == ConnectionState.Closed)
        //        {
        //            com.Connection.Open();
        //        }
        //        ds = new DataSet();
        //        SqlDataAdapter da = new SqlDataAdapter(com);
        //        da.Fill(ds);
        //        return ds;
        //    }
        //    catch (Exception ex)
        //    {
        //        return new DataSet();
        //        throw ex;
        //    }
        //    finally
        //    {
        //        com.Connection.Close();
        //    }
        //}
        public static Int32 INSERTTABLE(string ACTION, DONHANG_ENTITY dh)
        {
            SqlCommand com = new SqlCommand();
            DATABASE_CONTROL csdl = new DATABASE_CONTROL();
            try
            {

                com.Connection = csdl.CheckConnect();
                com.Parameters.AddWithValue("@ACTION", ACTION);
                if (dh != null)
                {
                    com.Parameters.AddWithValue("@MAHD", dh.MAHD);
                    com.Parameters.AddWithValue("@MAKH", dh.MAKH);
                    com.Parameters.AddWithValue("@NGAYHD", dh.NGAYHD);
                    com.Parameters.AddWithValue("@THANHTIEN", dh.THANHTIEN);
                }
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = sp_Name1;
                if (com.Connection.State == ConnectionState.Closed)
                {
                    com.Connection.Open();
                }
                return Convert.ToInt32(com.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                com.Connection.Close();
            }
        }
        public static Int32 INSERTTABLEDETAIL(string ACTION, CHITIET_DONHANG_ENTITY dh)
        {
            SqlCommand com = new SqlCommand();
            DATABASE_CONTROL csdl = new DATABASE_CONTROL();
            try
            {

                com.Connection = csdl.CheckConnect();
                com.Parameters.AddWithValue("@ACTION", ACTION);
                if (dh != null)
                {
                    com.Parameters.AddWithValue("@MAHD", dh.MAHD);
                    com.Parameters.AddWithValue("@MAXE", dh.MAXE);
                    com.Parameters.AddWithValue("@SOLUONG", dh.SOLUONG);
                    com.Parameters.AddWithValue("@DONGIA", dh.DONGIA);
                }
                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = sp_Name1;
                if (com.Connection.State == ConnectionState.Closed)
                {
                    com.Connection.Open();
                }
                return Convert.ToInt32(com.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                com.Connection.Close();
            }
        }
    }
